package com.sccba.emtbusiness.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sccba.common.exception.ServiceException;
import com.sccba.common.utils.DateUtils;
import com.sccba.common.utils.poi.ExcelUtil;
import com.sccba.emtbusiness.domain.YjczDutyPerson;
import com.sccba.emtbusiness.domain.YjczProductEventDetail;
import com.sccba.emtbusiness.domain.YjczProductEventInfo;
import com.sccba.emtbusiness.enums.NodeHandleStatus;
import com.sccba.emtbusiness.enums.ProductEventStatus;
import com.sccba.emtbusiness.service.IYjczProductEventDetailService;
import com.sccba.emtbusiness.service.IYjczProductEventInfoService;
import org.jxls.common.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczHandleTimeLineMapper;
import com.sccba.emtbusiness.domain.YjczHandleTimeLine;
import com.sccba.emtbusiness.service.IYjczHandleTimeLineService;

import javax.servlet.http.HttpServletResponse;

/**
 * 处理时间线记录Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-24
 */
@Service
public class YjczHandleTimeLineServiceImpl implements IYjczHandleTimeLineService 
{
    @Autowired
    private YjczHandleTimeLineMapper yjczHandleTimeLineMapper;

    @Autowired
    private IYjczProductEventInfoService yjczProductEventInfoService;

    @Autowired
    private IYjczProductEventDetailService productEventDetailService;

    /**
     * 查询处理时间线记录
     * 
     * @param pkId 处理时间线记录主键
     * @return 处理时间线记录
     */
    @Override
    public YjczHandleTimeLine selectYjczHandleTimeLineByPkId(Long pkId)
    {
        return yjczHandleTimeLineMapper.selectYjczHandleTimeLineByPkId(pkId);
    }

    /**
     * 查询处理时间线记录列表
     * 
     * @param yjczHandleTimeLine 处理时间线记录
     * @return 处理时间线记录
     */
    @Override
    public List<YjczHandleTimeLine> selectYjczHandleTimeLineList(YjczHandleTimeLine yjczHandleTimeLine)
    {
        return yjczHandleTimeLineMapper.selectYjczHandleTimeLineList(yjczHandleTimeLine);
    }

    /**
     * 新增处理时间线记录
     * 
     * @param yjczHandleTimeLine 处理时间线记录
     * @return 结果
     */
    @Override
    public int insertYjczHandleTimeLine(YjczHandleTimeLine yjczHandleTimeLine)
    {
        return yjczHandleTimeLineMapper.insertYjczHandleTimeLine(yjczHandleTimeLine);
    }

    /**
     * 修改处理时间线记录
     * 
     * @param yjczHandleTimeLine 处理时间线记录
     * @return 结果
     */
    @Override
    public int updateYjczHandleTimeLine(YjczHandleTimeLine yjczHandleTimeLine)
    {
        return yjczHandleTimeLineMapper.updateYjczHandleTimeLine(yjczHandleTimeLine);
    }

    /**
     * 批量删除处理时间线记录
     * 
     * @param pkIds 需要删除的处理时间线记录主键
     * @return 结果
     */
    @Override
    public int deleteYjczHandleTimeLineByPkIds(Long[] pkIds)
    {
        return yjczHandleTimeLineMapper.deleteYjczHandleTimeLineByPkIds(pkIds);
    }

    /**
     * 删除处理时间线记录信息
     * 
     * @param pkId 处理时间线记录主键
     * @return 结果
     */
    @Override
    public int deleteYjczHandleTimeLineByPkId(Long pkId)
    {
        return yjczHandleTimeLineMapper.deleteYjczHandleTimeLineByPkId(pkId);
    }

    @Override
    public void batchInsertTimeLine(List<YjczHandleTimeLine> list) {
        yjczHandleTimeLineMapper.batchInsertTimeLine(list);
    }

    @Override
    public List<YjczHandleTimeLine> selectListByEventId(Long eventId) {
        return yjczHandleTimeLineMapper.selectListByEventId(eventId);
    }

    @Override
    public void deleteByEventId(Long eventId) {
        yjczHandleTimeLineMapper.deleteByEventId(eventId);
    }

    @Override
    public void markDownLineNode(YjczHandleTimeLine handleTimeLine) {
        yjczHandleTimeLineMapper.updateByPkId(handleTimeLine);
        Long eventId = handleTimeLine.getHandleEventId();
        List<Integer> stateList = yjczHandleTimeLineMapper.selectStateListByEventId(eventId);
        int doneCount = 0;
        int doingCount = 0;
        for (Integer state : stateList) {
            if (null != state) {
                if (state >= NodeHandleStatus.NOT_DO.getCode()) {
                    doneCount ++;
                } else if (state == NodeHandleStatus.DOING.getCode()) {
                    doingCount ++;
                }
            }
        }
        int productEventState;
        if (doneCount == stateList.size()) {
            // TODO：根据补录信息决定是否赋值“待补录”
            YjczProductEventDetail eventDetail = productEventDetailService.selectDetailByEventId(eventId);
            if (null != eventDetail) {
                productEventState = ProductEventStatus.DONE.getCode();
            } else {
                productEventState = ProductEventStatus.WAIT_FILL.getCode();
            }
        } else {
            if (doingCount > 0 || doneCount > 0) {
                productEventState = ProductEventStatus.DOING.getCode();
            } else {
                productEventState = ProductEventStatus.WAIT_DO.getCode();
            }
        }
        yjczProductEventInfoService.updateEventState(productEventState, eventId);
    }

    @Override
    public void exportHandleTimeLine(HttpServletResponse response, YjczProductEventInfo eventInfo) {
        if (null == eventInfo) {
            throw new ServiceException("请选择正确的生产事件进行导出");
        }
        eventInfo = yjczProductEventInfoService.selectYjczProductEventInfoByPkId(eventInfo.getPkId());
        List<YjczHandleTimeLine> timeLineList = yjczHandleTimeLineMapper.selectListByEventId(eventInfo.getPkId());
        Context context = new Context();
        context.putVar("eventInfo", eventInfo);
        context.putVar("timeLineList", timeLineList);
        ExcelUtil excelUtil = new ExcelUtil(YjczHandleTimeLine.class);
        Map<String, Object> funcs = new HashMap<>();
        funcs.put("DateUtils", new DateUtils()); // 添加自定义功能
        excelUtil.exportExcel(response, context, "timeLineTemplate.xls", funcs);
    }
}
